import tkinter as tk
from tkinter import scrolledtext, filedialog, messagebox

class PatchPlusRemoverApp:
    def __init__(self, root):
        self.root = root
        self.root.title("补丁加号去除工具")
        self.root.geometry("800x600")
        self.root.resizable(True, True)
        
        # 设置字体确保中文显示正常
        self.default_font = ('SimHei', 10)
        
        self.create_widgets()
        
    def create_widgets(self):
        # 创建菜单栏
        menubar = tk.Menu(self.root)
        filemenu = tk.Menu(menubar, tearoff=0)
        filemenu.add_command(label="打开文件", command=self.open_file)
        filemenu.add_command(label="保存结果", command=self.save_result)
        filemenu.add_separator()
        filemenu.add_command(label="退出", command=self.root.quit)
        menubar.add_cascade(label="文件", menu=filemenu)
        self.root.config(menu=menubar)
        
        # 创建主框架
        main_frame = tk.Frame(self.root, padx=10, pady=10)
        main_frame.pack(fill=tk.BOTH, expand=True)
        
        # 创建输入区域
        input_frame = tk.LabelFrame(main_frame, text="输入补丁文本", font=self.default_font)
        input_frame.pack(fill=tk.BOTH, expand=True, pady=(0, 10))
        
        self.input_text = scrolledtext.ScrolledText(input_frame, wrap=tk.WORD, font=self.default_font)
        self.input_text.pack(fill=tk.BOTH, expand=True)
        
        # 创建按钮区域
        button_frame = tk.Frame(main_frame)
        button_frame.pack(fill=tk.X, pady=5)
        
        process_button = tk.Button(button_frame, text="处理补丁", command=self.process_patch, 
                                  font=self.default_font, bg="#4CAF50", fg="white")
        process_button.pack(side=tk.LEFT, padx=5)
        
        clear_button = tk.Button(button_frame, text="清空", command=self.clear_all, 
                                font=self.default_font, bg="#F44336", fg="white")
        clear_button.pack(side=tk.RIGHT, padx=5)
        
        # 创建输出区域
        output_frame = tk.LabelFrame(main_frame, text="处理结果", font=self.default_font)
        output_frame.pack(fill=tk.BOTH, expand=True, pady=(10, 0))
        
        self.output_text = scrolledtext.ScrolledText(output_frame, wrap=tk.WORD, font=self.default_font)
        self.output_text.pack(fill=tk.BOTH, expand=True)
        
        # 创建状态栏
        status_frame = tk.Frame(self.root, bd=1, relief=tk.SUNKEN)
        status_frame.pack(side=tk.BOTTOM, fill=tk.X)
        
        self.status_label = tk.Label(status_frame, text="就绪", anchor=tk.W, font=self.default_font)
        self.status_label.pack(side=tk.LEFT, padx=5, pady=2)
        
    def open_file(self):
        """打开补丁文件并加载到输入框"""
        file_path = filedialog.askopenfilename(
            filetypes=[("补丁文件", "*.patch *.diff"), ("所有文件", "*.*")]
        )
        if file_path:
            try:
                with open(file_path, 'r', encoding='utf-8') as file:
                    content = file.read()
                    self.input_text.delete(1.0, tk.END)
                    self.input_text.insert(tk.END, content)
                self.status_label.config(text=f"已加载文件: {file_path}")
            except Exception as e:
                messagebox.showerror("错误", f"无法打开文件: {str(e)}")
    
    def save_result(self):
        """保存处理结果到文件"""
        content = self.output_text.get(1.0, tk.END).strip()
        if not content:
            messagebox.showwarning("警告", "没有内容可保存")
            return
        
        file_path = filedialog.asksaveasfilename(
            defaultextension=".txt",
            filetypes=[("文本文件", "*.txt"), ("所有文件", "*.*")]
        )
        if file_path:
            try:
                with open(file_path, 'w', encoding='utf-8') as file:
                    file.write(content)
                self.status_label.config(text=f"已保存文件: {file_path}")
            except Exception as e:
                messagebox.showerror("错误", f"无法保存文件: {str(e)}")
    
    def process_patch(self):
        """处理补丁文本，去除每行开头的加号"""
        input_text = self.input_text.get(1.0, tk.END).strip()
        if not input_text:
            messagebox.showwarning("警告", "请先输入补丁文本")
            return
        
        try:
            processed_lines = []
            for line in input_text.split('\n'):
                # 只移除行首的+号，保留其他位置的+号
                if line.startswith('+'):
                    processed_lines.append(line[1:])
                else:
                    processed_lines.append(line)
            
            output_text = '\n'.join(processed_lines)
            self.output_text.delete(1.0, tk.END)
            self.output_text.insert(tk.END, output_text)
            
            self.status_label.config(text="处理完成")
        except Exception as e:
            messagebox.showerror("错误", f"处理失败: {str(e)}")
            self.status_label.config(text="处理失败")
    
    def clear_all(self):
        """清空所有文本框"""
        self.input_text.delete(1.0, tk.END)
        self.output_text.delete(1.0, tk.END)
        self.status_label.config(text="已清空所有内容")

if __name__ == "__main__":
    root = tk.Tk()
    app = PatchPlusRemoverApp(root)
    root.mainloop()